﻿using Brc_PaaS.Common.Entity;
using Brc_PaaS.Common.Helper;
using Brc_PaaS.Common.Model.Account;
using Brc_PaaS.Common.Model.Basic;
using Brc_PaaS.Dal;
using System;

namespace Brc_PaaS.Bll.Account
{
   public class Account : IAccount
    {
        public readonly IRepository<Bas_Account> accountRepository;

        public Account(IRepository<Bas_Account> accountRepository)
        {
            this.accountRepository = accountRepository;
        }

        public ResponseModel<string> RegisterAccount(RegisterAccountDto registerAccountDto)
        {
            var result = new ResponseModel<string> { IsSuccess = false };

            if (registerAccountDto == null)
            {
                result.Msg = "注册用户信息对象为空";
                return result;
            }

            // 检查用户名是否存在
            var accountInfoDto = GetAccount(registerAccountDto.LoginName);
            if (accountInfoDto != null && accountInfoDto.IsSuccess && accountInfoDto.Data != null)
            {
                result.Msg = "用户名已经存在";
                return result;
            }

            var accountInfo = new Bas_Account()
            {
                CreateTime = DateTime.Now,
                Id = Guid.NewGuid().ToString("N"),
                Status = 1,
                LoginName = registerAccountDto.LoginName,
                Passwords = registerAccountDto.Passwords,
                RealName = registerAccountDto.RealName,
                Tel = registerAccountDto.Tel
            };

            var affectNumber = accountRepository.AddItem(accountInfo);

            if (affectNumber <= 0)
            {
                result.Msg = "添加到数据库失败";
                return result;
            }

            result = new ResponseModel<string>()
            {
                IsSuccess = true,
                Data = accountInfo.Id
            };
            return result;

        }

        public ResponseModel<string> Login(LoginCredentialDto loginCredentialDto)
        {
            var result = new ResponseModel<string> { IsSuccess = false };

            if (loginCredentialDto == null)
            {
                result.Msg = "登录信息对象为空";
                return result;
            }
            var accountInfoDto = GetAccount(loginCredentialDto.LoginName);
            if (accountInfoDto == null || !accountInfoDto.IsSuccess || accountInfoDto.Data == null)
            {
                result.Msg = "未找到账号信息";
                return result;
            }

            if (loginCredentialDto.Passwords != accountInfoDto.Data.Passwords)
            {
                result.Msg = "密码错误";
                return result;
            }

            var token=JwtHelper.CreateJWT(loginCredentialDto.LoginName, loginCredentialDto.Passwords,accountInfoDto.Data.Id);

            result = new ResponseModel<string>()
            {
                IsSuccess = true,
                Data = token
            };
            return result;
        }

        public ResponseModel<Bas_Account> GetAccount(string loginName)
        {
            ResponseModel<Bas_Account> result = new ResponseModel<Bas_Account>() { IsSuccess = false, Msg = string.Empty, Data = new Bas_Account() };
            if (string.IsNullOrWhiteSpace(loginName))
            {
                result.Msg = "登录账号不能为空";
                return result;
            }
            result.Data = accountRepository.Get(i => i.LoginName == loginName);
            result.IsSuccess = true;
            return result;
        }


    }
}
